#!/bin/env python
#-*-coding:utf-8-*-   



import time
import datetime 
import os
import sys


def getYesterday():   
    today=datetime.date.today()   
    oneday=datetime.timedelta(days=1)   
    yesterday=today-oneday    
    return yesterday   
      
def getToday():   
    return datetime.date.today()     
 
#获取给定参数的前几天的日期，返回一个list   
def getDaysByNum(num):   
    today=datetime.date.today()   
    oneday=datetime.timedelta(days=1)       
    li=[]        
    for i in range(0,num):   
        #今天减一天，一天一天减   
        today=today-oneday   
        #把日期转换成字符串   
        #result=datetostr(today)   
        #li.append(datetostr(today))   
        li.append(today.strftime("%Y%m%d"))   
    return li   

def getDaysFromTo(from_date,to_date):   
    #today=datetime.date.today()   
    if from_date > to_date:
        return NONE
    oneday=datetime.timedelta(days=1)       
    li=[]        
    while from_date <= to_date:
        #今天减一天，一天一天减   
        li.append(datetime2timestamp(from_date))   
        from_date=from_date+oneday   
        #把日期转换成字符串   
        #result=datetostr(today)   
        #li.append(datetostr(today))   
    return li   
 
 
#将字符串转换成datetime类型   
def strtodatetime(datestr,format):       
    return datetime.datetime.strptime(datestr,format)   
 
#时间转换成字符串,格式为2008-08-02   
def datetostr(date):     
    return   str(date)[0:10]   

def date2str(date,format):
    return date.strftime(format)
 
#两个日期相隔多少天，例：2008-10-03和2008-10-01是相隔两天   
def datediff(beginDate,endDate):   
    format="%Y-%m-%d";   
    bd=strtodatetime(beginDate,format)   
    ed=strtodatetime(endDate,format)       
    oneday=datetime.timedelta(days=1)   
    count=0 
    while bd!=ed:   
        ed=ed-oneday   
        count+=1 
    return count   
def datediffbydatetime(bd,ed):
    oneday=datetime.timedelta(days=1)   
    count=0 
    while bd < ed:   
        ed=ed-oneday   
        count+=1 
    return count   
 
#获取两个时间段的所有时间,返回list   
def getDays(beginDate,endDate):   
    format="%Y-%m-%d";   
    bd=strtodatetime(beginDate,format)   
    ed=strtodatetime(endDate,format)   
    oneday=datetime.timedelta(days=1)    
    num=datediff(beginDate,endDate)+1    
    li=[]   
    for i in range(0,num):    
        li.append(datetostr(ed))   
        ed=ed-oneday   
    return li   
 
#获取当前年份 是一个字符串   
def getYear():   
    return str(datetime.date.today())[0:4]    
 
#获取当前月份 是一个字符串   
def getMonth():   
    return str(datetime.date.today())[5:7]   
 
#获取当前天 是一个字符串   
def getDay():   
    return str(datetime.date.today())[8:10]      

def getNow():   
    return datetime.datetime.now()   

def timestamp2datetime(ts):
    return datetime.datetime.fromtimestamp(float(ts))

def datetime2timestamp(dt):
    return int(time.mktime(dt.timetuple()))

def datediffbydatetime(bd,ed):
    #oneday=datetime.timedelta(days=1)   
    return ed-bd
    '''count=0 
    while bd < ed:   
        ed=ed-oneday   
        count+=1 
    return count
    '''

def timedelta(days):
    return datetime.timedelta(days=days)
       
def main():
    a = strtodatetime("20130623","%Y%m%d")
    print datetime2timestamp(a)
    #dt = Date()
    #print dt.getYesterday().strftime("%Y%m%d")
    #print dt.timestamp2datetime('1334637093')
    #print dt.datetime2timestamp(dt.strtodatetime("2011-4-7","%Y-%m-%d"))


    pass
'''
sec="1334637093"
print time.localtime(int(sec))
print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(sec)))
day = datetime.datetime(2012,1,10)
print day.timetuple()
print type(time.mktime(day.timetuple()))
print float(sec)
print datetime.datetime.fromtimestamp(float(sec))
print day
'''


if __name__ == "__main__":
    main()
        
# vim: et ts=4 sts=4 sw=4



